package love.yuanmeng.guetcoalsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import love.yuanmeng.guetcoalsystem.mapper.*;
import love.yuanmeng.guetcoalsystem.pojo.*;
import love.yuanmeng.guetcoalsystem.service.PropService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import love.yuanmeng.guetcoalsystem.util.RandomUtils;
import love.yuanmeng.guetcoalsystem.vo.PropAndPage;
import love.yuanmeng.guetcoalsystem.vo.PropAuditVo;
import love.yuanmeng.guetcoalsystem.vo.PropIdEnterIdVo;
import love.yuanmeng.guetcoalsystem.vo.PropManagerVo;
import love.yuanmeng.guetcoalsystem.vo.PropVo;
import love.yuanmeng.guetcoalsystem.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PropServiceImpl implements PropService {
    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR = "ERROR";

    @Resource
    private PropMapper propMapper;
    @Resource
    private PropVoMapper propVoMapper;
    @Resource
    private PropManagerVoMapper propManagerVoMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    CBondMapper cBondMapper;
    @Resource
    UserMapper userMapper;


    @Resource
    private PropAuditVoMapper propAuditVoMapper;
    @Autowired
    MessageService messageService;

    /**
     * 卖方挂牌
     *
     * @param data 实体类数据
     * @return SUCCESS表示成功，ERROR表示失败
     */
    @Override
    public String addProp(Prop data) {
        data.setPropstatus("0");
        data.setCoalstatus("0");
        if (propMapper.insert(data) == 1) {
            return SUCCESS;
        }
        return ERROR;
    }

    /**
     * 卖方挂牌，新增牌子
     *
     * @param
     * @return
     */

    @Override
    public int addPropAnduserid(Prop prop) {
        //缴纳保证金
        float su = Float.valueOf(prop.getSupplyQuantity());
        float bondNumber = Float.valueOf(prop.getBondNumber());


        User user = userMapper.selectById(prop.getUserid());

        QueryWrapper<CBond> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", user.getEnterpriseid());
        CBond cBond = cBondMapper.selectOne(queryWrapper);
        float sum = su * bondNumber * 10000;
        float myBond = Float.valueOf(cBond.getUnfrozenAmount());
        float myBondCon = Float.valueOf(cBond.getContractFrozenAmount());

        if (myBond > sum) {//大于则允许进行缴纳保证金
            cBond.setUnfrozenAmount(String.valueOf(myBond - sum));
            System.out.println("未冻结："+(myBond - sum));
            cBond.setContractFrozenAmount(String.valueOf(myBondCon + sum));
            System.out.println("要冻结："+(myBondCon + sum));
            cBondMapper.updateById(cBond);
        }

        int i = 0;
        //创建牌子
        i = propMapper.insert(prop);


        return i;
    }

    /**
     * 买方摘牌
     *
     * @param data 实体ID
     * @return SUCCESS表示成功，ERROR表示失败
     */
    @Override
    public int updateProp(PropIdEnterIdVo data) {
        Prop prop = propMapper.selectById(data.getPropId());
        if ("1".equals(prop.getPropstatus())) {
            //牌子已经是被摘了的状态了！
            return 0;
        }
        if (!"0".equals(prop.getPropstatus())) {//如果牌子不是在挂状态，不允许摘牌
            return 0;
        }

        prop.setPropstatus("1");
        Order order = new Order();
        order.setOrderId(null);
        order.setStatus("0");
        order.setCreateDate(RandomUtils.getTime());
        order.setEnterpriseId1(data.getEnterpriseIdSale());
        order.setEnterpriseId2(data.getEnterpriseIdBuy());
        order.setPropId(data.getPropId());
        float num = Float.valueOf(prop.getSupplyQuantity());
        float unitPrice = Float.valueOf(prop.getUnitPrice());


        order.setPrice(String.valueOf(num * unitPrice * 10000));
        orderMapper.insert(order);

        int i = propMapper.updateById(prop);
        if (i == 1)
            return 1;
        else
            return 0;
    }


    /**
     * 交易中心查询
     *
     * @param
     * @return DataGridView 视图
     */
    @Override
    public DataGridView loadPropByStatus() {

        List<PropVo> list = this.propVoMapper.loadAllPropByStatusToVo();
        return new DataGridView(new Long(list.size()), list);

    }

    /**
     * 根据牌子ID获取牌子信息
     *
     * @param propId 牌子ID
     * @return 牌子信息
     */
    @Override
    public DataGridView getPropById(String propId) {
        System.out.println(propId);
        DataGridView data = new DataGridView();
        data.setData(propMapper.selectById(propId));
        return data;
    }

    /**
     * 卖方个人牌子查询
     *
     * @param
     * @return
     */
    @Override
    public DataGridView loadPropByStatusAndOrder(String userid) {

        List<PropManagerVo> list = this.propManagerVoMapper.loadProp(userid);
        return new DataGridView(new Long(list.size()), list);

    }


    /**
     * 挂牌审核数据列表
     *
     * @param propAndPage
     * @param
     * @return
     */
    @Override
    public List<PropAuditVo> getPropAuditVoList(PropAndPage propAndPage) {
        return propAuditVoMapper.getPropAuditVoList(propAndPage);
    }


    /**
     * 审核牌子
     *
     * @param prop
     * @param message
     * @return
     */
    @Override
    public boolean approvedProp(Prop prop, Message message) {
        UpdateWrapper<Prop> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", prop.getId()).set("propstatus", prop.getPropstatus());
        return propMapper.update(null, wrapper) == 1 && messageService.inserMessage(message);
    }


    /**
     * 卖方下架牌子
     *
     * @param
     * @return
     */
    @Override
    public int updatePropStatus(Prop prop) {
        prop.setPropstatus("4");

        int i = propMapper.updateById(prop);

        return i;
    }

    /**
     * 卖方发货
     *
     * @param
     * @return
     */
    @Override
    public int updatePropCoalStatus(Prop prop) {
        prop.setCoalstatus("已发货");
        int i = propMapper.updateById(prop);
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("prop_id", prop.getId());
        Order order = new Order();
        order.setStatus("2");
        orderMapper.update(order, updateWrapper);

        return i;
    }
}
